package com.qcxk.yuqian.system.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcxk.yuqian.system.entity.User;
import com.qcxk.yuqian.system.entity.UserRole;
import com.qcxk.yuqian.system.mapper.RoleMapper;
import com.qcxk.yuqian.system.mapper.UserMapper;
import com.qcxk.yuqian.system.mapper.UserRoleMapper;
import com.qcxk.yuqian.system.service.UserService;
import com.qcxk.yuqian.system.utils.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImp extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Page queryUserPage(Page page, QueryWrapper<User> userQueryWrapper) {

//        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//        if(StringUtils.isNotBlank(user.getName())){
//            userQueryWrapper.like("name",user.getName());
//        }
//        if(StringUtils.isNotBlank(user.getPhone())){
//            userQueryWrapper.like("phone",user.getPhone());
//        }
//        if(StringUtils.isNotBlank(user.getQq())){
//            userQueryWrapper.like("qq",user.getQq());
//        }
//        if(StringUtils.isNotBlank(user.getState())){
//            userQueryWrapper.eq("state",user.getState());
//        }
//        if(StringUtils.isNotBlank(user.getType())){
//            userQueryWrapper.eq("type",user.getType());
//        }

        userMapper.selectPage(page, userQueryWrapper);

        return page;
    }

    @Override
    public boolean updateUser(User user) {
        User item = userMapper.selectById(user.getId());
        if (StringUtils.isNotBlank(user.getState())) {
            item.setState(user.getState());
        }
        if (user.getDeptId() != null) {
            item.setDeptId(user.getDeptId());
        }
        if (StringUtils.isNotBlank(user.getHeadpic())) {
            item.setHeadpic(user.getHeadpic());
        }
        if (StringUtils.isNotBlank(user.getFullName())) {
            item.setFullName(user.getFullName());
        }
        if (StringUtils.isNotBlank(user.getQq())) {
            item.setQq(user.getQq());
        }
        if (StringUtils.isNotBlank(user.getPhone())) {
            item.setPhone(user.getPhone());
        }
        if (StringUtils.isNotBlank(user.getWx())) {
            item.setWx(user.getWx());
        }
        if (StringUtils.isNotBlank(user.getWxqrcode())) {
            item.setWxqrcode(user.getWxqrcode());
        }

        if (StringUtils.isNotBlank(user.getGender())) {
            item.setGender(user.getGender());
        }
        if (StringUtils.isNotBlank(user.getBirth())) {
            item.setBirth(user.getBirth());
        }
        if (StringUtils.isNotBlank(user.getPartyDate())) {
            item.setPartyDate(user.getPartyDate());
        }
        if (StringUtils.isNotBlank(user.getHometown())) {
            item.setHometown(user.getHometown());
        }
        if (StringUtils.isNotBlank(user.getGraduateSchool())) {
            item.setGraduateSchool(user.getGraduateSchool());
        }
        if (StringUtils.isNotBlank(user.getEducation())) {
            item.setEducation(user.getEducation());
        }
        if (StringUtils.isNotBlank(user.getProfessional())) {
            item.setProfessional(user.getProfessional());
        }
        if (StringUtils.isNotBlank(user.getWorkPro())) {
            item.setWorkPro(user.getWorkPro());
        }
        if (StringUtils.isNotBlank(user.getMarriage())) {

            item.setMarriage(user.getMarriage());
        }
        if (StringUtils.isNotBlank(user.getPassword())) {
            if(!item.getPassword().equals(user.getPassword())){
                item.setPassword(user.getPassword());

            }
        }
        if (user.getOrganId()!=null) {
            item.setOrganId(user.getOrganId());
        }
        int re = userMapper.updateById(item);
        return re == 1 ? true : false;
    }

    @Override
    public boolean deleteUserById(User user) {
        int re = userMapper.deleteById(user.getId());
        if (re == 1) {
            re = userRoleMapper.delete(new QueryWrapper<UserRole>().eq("user_id", user.getId()));
            return true;
        }
        return false;
    }

    @Override
    public Page queryUserForAdminPage(Page page, User item) {
        page = userMapper.queryUserForAdminPage(page, item);
        return page;
    }

    @Override
    public List<User> queryUserForMaterialRequisition() {
        return userMapper.queryUserForMaterialRequisition();
    }

}
